package yxy.game.pm2.active.command.ghost;

import yxy.apple.logger.Logger;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_神魔卡;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.active.module.subordinateclass.ghost.GhostGachaTask;
import yxy.game.pm2.bean.task.Task;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;

/**
 * 神魔卡_抽将任务
 * @author Dell
 *
 */
@MessageType(proto = Command.ActGhostGachaTaskInfo, classtype = ActiveMessage.ActGhostGachaTaskInfoRequest.class)
public class CMD_Active_神魔卡_抽将任务_Info extends Command<ActiveMessage.ActGhostGachaTaskInfoRequest> {

	private static final Logger logger = Logger.create(CMD_Active_神魔卡_抽将任务_Info.class);
	
	@Override
	public void execute(MessageSource messageSource, ActiveMessage.ActGhostGachaTaskInfoRequest request) throws Exception {
		String playercode = messageSource.getTag();
		ActiveMessage.ActGhostGachaTaskInfoResponse.Builder response = ActiveMessage.ActGhostGachaTaskInfoResponse.newBuilder();
		
		ActiveModule_神魔卡 am = ActiveModule_神魔卡.get(playercode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}

		if(!am.running()) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}

		for(GhostGachaTask gt : am.getGhostmap().values()) {
			ActiveMessage.GachaTask.Builder build = ActiveMessage.GachaTask.newBuilder();
			build.setId(gt.getId());
			build.setTaskid(gt.getTaskid());
			
			String taskcode = gt.getTaskcode();
			Task taskbean = Task.get(playercode, taskcode);
			build.setProgress(taskbean.getProgress());
			int canget = taskbean.isReward() ? DrawStatus.hadget.getStatus() : taskbean.isFinish() ? DrawStatus.canget.getStatus() : DrawStatus.cannotget.getStatus();
			build.setCanget(canget);

			response.addGachatask(build.build());
		}
		
		response.setStart(am.getTimeline().getStartTime().getMillis());
		response.setEnd(am.getTimeline().getEndTime().getMillis());
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActGhostGachaTaskInfoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
